package com.example.a79273.shandianban;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a79273.shandianban.BaseActivity;
import com.example.a79273.shandianban.R;
import com.example.a79273.shandianban.application.MyApplication;
import com.example.a79273.shandianban.db.Login;
import com.example.a79273.shandianban.view.RegisterActivity;

import org.xutils.DbManager;
import org.xutils.db.Selector;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.util.List;
import java.util.concurrent.CancellationException;

public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private DbManager dbManager;
    private View mViewTablayout;
    private ImageView mImageViewBack;
    private TextView mTextViewTitlte,tv_login_register;
    private EditText et_login_number, et_login_pwd;
    private String pass;
    private String name;
    private MyApplication myApplication;
    private Button bt_login_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {
        mImageViewBack.setOnClickListener(this);
        tv_login_register.setOnClickListener(this);

    }

    private void setData() {
        mTextViewTitlte.setText("登录");

    }


    private void initData() {
        DbManager.DaoConfig daoConfig = new DbManager.DaoConfig();
        dbManager = x.getDb(daoConfig);

    }

    private void initView() {
        mViewTablayout = findViewById(R.id.in_login_tablayout);
        mImageViewBack = (ImageView) mViewTablayout.findViewById(R.id
                .iv_tablayout_back);
        mTextViewTitlte = (TextView) mViewTablayout.findViewById(R.id
                .tv_tablayout_title);
        et_login_number = (EditText) findViewById(R.id.et_login_number);
        et_login_pwd = (EditText) findViewById(R.id.et_login_pwd);
        tv_login_register = (TextView)findViewById(R.id.tv_login_register);
    }

    //所有的点击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_tablayout_back:
                finish();
                break;
            case R.id.bt_login_login:
                pass = et_login_pwd.getText().toString().trim();
                name = et_login_number.getText().toString().trim();
                try {
                    List<Login> all = dbManager.selector(Login.class).where
                            ("username", "=", name).and("passsword", "=", pass).findAll();
                    if(all!=null&&all.size()>0){
                        myApplication.setLogin(true);
                        Intent intent=new Intent(this,MainActivity.class);
                        startActivity(intent);
                    }else{
                        Toast.makeText(LoginActivity.this, "用户名不存在,请注册", Toast.LENGTH_SHORT).show();
                    }

                } catch (DbException e) {
                    e.printStackTrace();
                }
                break;
            case R.id.tv_login_register:
                    Intent intent=new Intent(this, RegisterActivity.class);
                    startActivity(intent);

                break;
        }
    }
}
